﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ConfigPanels : System.Web.UI.Page
{
    ServerObject mServerObject = null;
    string mTableName = "ConfigPanels";
    public string mTitlePage = "Configurarea panelurilor";
    string objDelimiter = Constants.Constants.objectDelimiter;
    char charObjDelimiter = char.Parse(Constants.Constants.objectDelimiter);
    object obj_1_TypeKey = "ObjectType";
    object obj_2_ValueKey = "TableValue";
    object obj_3_RowsKey = "TableKey";
    object obj_4_RowsName = "TableColumnName";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (mServerObject == null) mServerObject = new ServerObject();
        if (Utils.AutentificatedUser)
        {
            if (!IsPostBack)
            {
                ShowPanel(listPanel.ID);
            }
        }
        else
        {
            ShowPanel(string.Empty);
        }
    }

    private void ShowPanel(string panelName)
    {
        #region Hide panels

        emptyPanel.Visible = false;
        listPanel.Visible = false;
        newPanel.Visible = false;
        editPanel.Visible = false;

        #endregion Hide panels

        try
        {
            #region Get Panel Name

            switch (panelName)
            {
                case "listPanel":
                    listPanel.Visible = true;

                    FillGeneralGridView();

                    break;

                case "newPanel":
                    newPanel.Visible = true;
                    {
                        FillAddTableList();
                        FillAddObjectList();
                    }

                    break;

                case "editPanel":
                    editPanel.Visible = true;
                    {
                        FillEditTableList();
                        FillEditObjectList();
                    }

                    break;

                default:
                    emptyPanel.Visible = true;
                    break;
            }

            #endregion Get Panel Name

        }
        catch (Exception ex)
        {
            Utils.InfoText(this, "Atentie! Eroare in sistem!", ex.Message);
        }
    }
        
    #region listPanel

    protected void FillGeneralGridView()    //listPanel
    {
        string csvColumns = "PanelName, TableName, csvInsert, csvUpdate, csvSelect, csvObject, csvAlias";
        string filterSelect = " isB=0";
        DataTable sourceTable = mServerObject.UniversalGetFromSingleTable(mTableName, csvColumns, filterSelect);
        generalGridView.DataSource = sourceTable;
        generalGridView.DataBind();
    }

    protected void addNewButton_Click(object sender, EventArgs e)
    {
        ClearAddNewPanel();
        ShowPanel(newPanel.ID);
    }

    protected void generalGridView_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        generalGridView.PageIndex = e.NewPageIndex;
        FillGeneralGridView();
    }

    protected void generalGridView_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";

            for (int i = 0; i < e.Row.Cells.Count - 1; i++)
            {
                e.Row.Cells[i].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.generalGridView, "Select$" + e.Row.RowIndex);
            }
        }
    }

    protected void generalGridView_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
    {
        int index = e.RowIndex;

        string whereCondition = "PanelName='" + generalGridView.Rows[index].Cells[0].Text+"' ";

        if (mServerObject.UniversalDeleteFromValue(mTableName, whereCondition))
        {
            ShowPanel(listPanel.ID);
        }
        else
        {
            Utils.InfoText(this, "Atentie", " { " + generalGridView.Rows[index].Cells[0].Text + " } - nu a fost sters. Verificati conectiunea cu internet ");
        }

    }

    protected void generalGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            ClearEditPanel();
            ShowPanel(editPanel.ID);

            GridViewRow row = generalGridView.SelectedRow;

            editPanelNameTextBox.Text = row.Cells[0].Text;
            editPanelNameTextBox.Enabled = false;

            editTableDDL.SelectedValue = row.Cells[1].Text;
            editTableDDL.Enabled = false;

            FillEditColumnsGridView();

            string csvAlias = row.Cells[2].Text;
            string csvInsert = row.Cells[3].Text.Replace("&nbsp;",string.Empty);
            string csvUpdate = row.Cells[4].Text.Replace("&nbsp;",string.Empty);
            string csvSelect = row.Cells[5].Text.Replace("&nbsp;",string.Empty);
            string csvObjects = row.Cells[6].Text;

            csvEditInsertTextBox.Text = csvInsert;
            csvEditUpdateTextBox.Text = csvUpdate;
            csvEditSelectTextBox.Text = csvSelect;
            
            List<string> listResultForAlias = new List<string>(csvAlias.Split(','));
            List<string> listResultForObjects = new List<string>(csvObjects.Split('|'));

            for (int indexRow = 0; indexRow < editColumnsGridView.Rows.Count; indexRow++)
            {
                string columnName = editColumnsGridView.Rows[indexRow].Cells[0].Text;

                #region Objects

                string listNameObjects = listResultForObjects[indexRow];
                List<string> subList = new List<string>(listNameObjects.Split('='));

                TextBox editObjectTextBox = editColumnsGridView.Rows[indexRow].FindControl("editObjectTextBoxID") as TextBox;
                editObjectTextBox.Text = subList[1];

                #endregion Objects

                #region alias

                string resultAliasValue = string.Empty;

                string listName = listResultForAlias[indexRow];

                if (!columnName.Equals(listName))
                {
                    resultAliasValue = listName;
                }

                TextBox editAliasTextBox = editColumnsGridView.Rows[indexRow].FindControl("editAliasTextBoxID") as TextBox;

                editAliasTextBox.Text = resultAliasValue;

                #endregion alias

                #region CheckBox

                FillEditCheckBox(indexRow, "editInsertCheckBoxID", csvEditInsertTextBox.ClientID, columnName, csvEditInsertTextBox.Text.Contains(columnName));

                FillEditCheckBox(indexRow, "editUpdateCheckBoxID", csvEditUpdateTextBox.ClientID, columnName, csvEditUpdateTextBox.Text.Contains(columnName));

                FillEditCheckBox(indexRow, "editSelectCheckBoxID", csvEditSelectTextBox.ClientID, columnName, csvEditSelectTextBox.Text.Contains(columnName));

                #endregion CheckBox
            }
        }
        catch (Exception ex)
        {
            Utils.InfoText(this, "Erroare", ex.Message);
        }      

        //editClassifierList
       
    }

    private void FillEditCheckBox(int indexRow, string nameCheckBoxID, string textBoxID, string columnName, bool isChecked)
    {
        CheckBox editCheckBox = editColumnsGridView.Rows[indexRow].FindControl(nameCheckBoxID) as CheckBox;
        editCheckBox.Checked = isChecked;

        string scriptForCheckBox = "DoTheClick(this.id,'" + textBoxID + "','" + columnName + "')";
        editCheckBox.Attributes.Add("onclick", scriptForCheckBox);
    }          

    #endregion listPanel

    #region newPanel

    private void FillAddTableList()    //newPanel
    {
        try
        {
            DataTable sourceTable = GetListTableFromBD();
           
            Utils.FillSelector(addNewTableDDL, sourceTable, "name", "name");
           
            addNewTableDDL.SelectedValue = "**";
        }
        catch (Exception ex)
        {
            //todo
        }
    }

    private void FillNewColumnsGridView()  //newPanel
    {        
        string selectedValue = addNewTableDDL.SelectedValue.ToString();
        FillColumnsGridView(selectedValue, columnsGridView);
    }

    private void FillAddObjectList()    //newPanel
    {
        try
        {
            DataTable sourceTable = new DataTable();
            sourceTable.Columns.Add("ObjectName", typeof(string));
            sourceTable.Columns.Add("ObjectID", typeof(int));

            sourceTable.NewRow();

            sourceTable.Rows.Add(Constants.Constants.ToolBox.TextBox.ToString(), Constants.Constants.ToolBox.TextBox);
            sourceTable.Rows.Add(Constants.Constants.ToolBox.Classifiers.ToString(), Constants.Constants.ToolBox.Classifiers);

            sourceTable.Rows.Add(Constants.Constants.ToolBox.CheckBox.ToString(), Constants.Constants.ToolBox.CheckBox);
            sourceTable.Rows.Add(Constants.Constants.ToolBox.DateTime.ToString(), Constants.Constants.ToolBox.DateTime);
            sourceTable.Rows.Add(Constants.Constants.ToolBox.Table.ToString(), Constants.Constants.ToolBox.Table);

            sourceTable.Rows.Add(Constants.Constants.ToolBox.SQL.ToString(), Constants.Constants.ToolBox.SQL);

            Utils.FillSelector(addObjectDropDownList, sourceTable, "ObjectName", "ObjectID");
        }
        catch (Exception ex)
        {
            //todo
        }
    }

    private void ClearAddNewPanel() //newPanel
    {
        addNewTableDDL.SelectedIndex = -1;

        addNewPanelNameTextBox.Text = string.Empty;
        csvInsertTextBox.Text = string.Empty;
        csvSelectTextBox.Text = string.Empty;
        csvUpdateTextBox.Text = string.Empty;
    }

    protected void addNewSaveButton_Click(object sender, EventArgs e)   //newPanel
    {
        if (CheckAddValid())
        {
            string panelName = addNewPanelNameTextBox.Text.Trim();
            
            string csvColumns = "PanelName, csvInsert, csvUpdate, csvSelect, csvObject, csvAlias, TableName";

            string csvInsert = csvInsertTextBox.Text;
            string csvUpdate = csvUpdateTextBox.Text;
            string csvSelect = csvSelectTextBox.Text;
            string csvObject = GetObjectsList(columnsGridView, "addObjectTextBoxID");
            string csvAlias = GetAliasList(columnsGridView, "aliasTextBoxID");

            string tableName = addNewTableDDL.SelectedValue.ToString();

            string csvValues = "'" + panelName + "','" + csvInsert + "','" + csvUpdate + "','" + csvSelect + "','" + csvObject + "','" + csvAlias + "','"+tableName+"'";
            
            Hashtable insertList = new Hashtable();
            insertList.Add(Utils.hashtableKeyInsertColumns, csvColumns);
            insertList.Add(Utils.hashtableKeyInsertValues, csvValues);

            if (mServerObject.UniversalInsertFromValue(mTableName, insertList))
            {
                ShowPanel(listPanel.ID);
            }
            else
            {
                Utils.InfoText(this, "Atentie", " { " + panelName + " } - nu a fost salvat. Verificati conectiunea cu internet ");
            }
        }
        else
        {
            Utils.InfoText(this, "Atentie", " Inserati toate cimpurile obligatorii !");
        }
    }

    private bool CheckAddValid()  //newPanel
    {
        bool result = true;
        int countFalse = 0;
        
        if (string.IsNullOrEmpty(addNewPanelNameTextBox.Text))
        {
            countFalse++;
        }
        
        if (string.IsNullOrEmpty(csvInsertTextBox.Text))
        {
            countFalse++;
        }

        if (string.IsNullOrEmpty(csvUpdateTextBox.Text))
        {
            countFalse++;
        }

        if (string.IsNullOrEmpty(csvSelectTextBox.Text))
        {
            countFalse++;
        }

        if (countFalse != 0)
        {
            result = false;
        }
        return result;
    }

    protected void addNewCancelButton_Click(object sender, EventArgs e) //newPanel
    {
        ShowPanel(listPanel.ID);
    }
    
    protected void addNewTableDDL_SelectedIndexChanged(object sender, EventArgs e)  //newPanel
    {
        FillNewColumnsGridView();
        ClearAddCSVTextBox();
    }

    private void ClearAddCSVTextBox()
    {
        csvInsertTextBox.Text = string.Empty;
        csvUpdateTextBox.Text = string.Empty;
        csvSelectTextBox.Text = string.Empty;
    }

    protected void addObjectDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectedValue = addObjectDropDownList.SelectedValue.ToString();
        int selectedValueID = 0;

        int.TryParse(selectedValue, out selectedValueID);

        bool visibleTable = false;
        pKeyID.Visible = false;
        pColumnNameID.Visible = false;
        pSqlID.Visible = false;

        DataTable sourceTable = new DataTable();

        switch (selectedValueID)
        {
            case (int)Constants.Constants.ToolBox.Classifiers:
                visibleTable = true;
                sourceTable = null;
                addTableDropDownList.DataTextField = "Name";
                addTableDropDownList.DataValueField = "Type ID";
                sourceTable = Utils.ModuleMain().GetClassifierTypesList();
                Utils.FillSelector(addTableDropDownList, sourceTable, "Name", "Type ID");
               
                break;

            case (int)Constants.Constants.ToolBox.TextBox:
                break;

            case (int)Constants.Constants.ToolBox.CheckBox:
                break;

            case (int)Constants.Constants.ToolBox.DateTime:
                break;

            case (int)Constants.Constants.ToolBox.Table:
                #region Table
                visibleTable = true;

                addTableDropDownList.DataTextField = "Name";
                addTableDropDownList.DataValueField = "Name";
                sourceTable = null;
                sourceTable = Load_TableDropDown();
                Utils.FillSelector(addTableDropDownList, sourceTable, "Name", "Name");
                #endregion Table
                break;

            case (int)Constants.Constants.ToolBox.SQL:
                #region SQL
                pSqlID.Visible = true;
                #endregion SQL
                break;

            default:
                break;
        }

        ShowHideAddObjectPanels(visibleTable);
    }

    private void ShowHideAddObjectPanels(bool visibleTable)
    {
        pTableID.Visible = visibleTable;
    }

    protected void addTableDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        string objectValue = addObjectDropDownList.SelectedValue.ToString();
        int objectID = 0;
        int.TryParse(objectValue, out objectID);

        pKeyID.Visible = false;
        pColumnNameID.Visible = false;

        if (objectID != (int)Constants.Constants.ToolBox.Classifiers)
        {  
            string selectedValue = addTableDropDownList.SelectedValue.ToString();
            int selectedValueID = 0;
            int.TryParse(selectedValue, out selectedValueID);

            pKeyID.Visible = true;
            pColumnNameID.Visible = true;

            DataTable sourceTable = mServerObject.GetColumnsByTableName(selectedValue);

            Utils.FillSelector(addKeyRowsDropDownList, sourceTable, "column_name", "column_name");
            Utils.FillSelector(addNameRowsDropDownList, sourceTable, "column_name", "column_name");
        }
    }
    
    protected void objectGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        // If multiple ButtonField column fields are used, use the
        // CommandName property to determine which button was clicked.
        switch (e.CommandName)
        {
            case "Select":
                #region Select
                // Convert the row index stored in the CommandArgument
                // property to an Integer.
                addObjectDivID.Visible = true;
                int index = Convert.ToInt32(e.CommandArgument);

                // Get the last name of the selected author from the appropriate
                // cell in the GridView control.
                GridViewRow selectedRow = columnsGridView.Rows[index];
                int countColumn = columnsGridView.Columns.Count;
                TextBox objectTextBoxID = (TextBox)selectedRow.Cells[countColumn-1].FindControl("addObjectTextBoxID");
                if(objectTextBoxID !=null)
                {
                    addColumnNameLabelID.Text = selectedRow.Cells[0].Text;
                    addObjectHiddenFieldID.Value = index.ToString();

                    #region 
                    List<string> listObject = new List<string>(objectTextBoxID.Text.Replace("'","").Trim().Split(charObjDelimiter));
                    Hashtable hashObject = new Hashtable();
                    hashObject.Add(obj_1_TypeKey, listObject[0]);
                    hashObject.Add(obj_2_ValueKey, listObject[1]);
                    hashObject.Add(obj_3_RowsKey, listObject[2]);
                    hashObject.Add(obj_4_RowsName, listObject[3]);
                    
                    try
                    {
                       int typeID = 0;
                       string typeObject = hashObject[obj_1_TypeKey].ToString();

                       #region
                       if (typeObject.Equals(Constants.Constants.ToolBox.TextBox.ToString()))
                       {
                           typeID = (int)Constants.Constants.ToolBox.TextBox;
                       }
                       if (typeObject.Equals(Constants.Constants.ToolBox.CheckBox.ToString()))
                       {
                           typeID = (int)Constants.Constants.ToolBox.CheckBox;
                       }
                       if (typeObject.Equals(Constants.Constants.ToolBox.DateTime.ToString()))
                       {
                           typeID = (int)Constants.Constants.ToolBox.DateTime;
                       }
                       if (typeObject.Equals(Constants.Constants.ToolBox.Classifiers.ToString()))
                       {
                           typeID = (int)Constants.Constants.ToolBox.Classifiers;
                       }
                       if (typeObject.Equals(Constants.Constants.ToolBox.Table.ToString()))
                       {
                           typeID = (int)Constants.Constants.ToolBox.Table;
                       }
                       if (typeObject.Equals(Constants.Constants.ToolBox.SQL.ToString()))
                       {
                           typeID = (int)Constants.Constants.ToolBox.SQL;
                       }
                       #endregion

                       bool visibleTable = false;
                       pKeyID.Visible = false;
                       pColumnNameID.Visible = false;
                       pSqlID.Visible = false;

                       DataTable sourceTable = new DataTable();

                       addObjectDropDownList.SelectedValue = typeID.ToString();

                       switch (typeID)
                       {
                           case (int)Constants.Constants.ToolBox.Classifiers:
                                visibleTable = true;
                                sourceTable = null;
                                addTableDropDownList.DataTextField = "Name";
                                addTableDropDownList.DataValueField = "Type ID";
                                sourceTable = Utils.ModuleMain().GetClassifierTypesList();
                                Utils.FillSelector(addTableDropDownList, sourceTable, "Name", "Type ID");

                                addTableDropDownList.SelectedValue = hashObject[obj_2_ValueKey].ToString();
                               break;

                           case (int)Constants.Constants.ToolBox.TextBox:
                               break;

                           case (int)Constants.Constants.ToolBox.CheckBox:
                               break;

                           case (int)Constants.Constants.ToolBox.DateTime:
                               break;

                           case (int)Constants.Constants.ToolBox.Table:
                               #region Table
                                visibleTable = true;

                                addTableDropDownList.DataTextField = "Name";
                                addTableDropDownList.DataValueField = "Name";
                                sourceTable = null;
                                sourceTable = Load_TableDropDown();
                                Utils.FillSelector(addTableDropDownList, sourceTable, "Name", "Name");

                                addTableDropDownList.SelectedValue = hashObject[obj_2_ValueKey].ToString().Trim();
                                                             
                                pKeyID.Visible = true;
                                pColumnNameID.Visible = true;

                                DataTable sourceTableRows = mServerObject.GetColumnsByTableName(hashObject[obj_2_ValueKey].ToString().Replace("'",string.Empty));

                                Utils.FillSelector(addKeyRowsDropDownList, sourceTableRows, "column_name", "column_name");
                                Utils.FillSelector(addNameRowsDropDownList, sourceTableRows, "column_name", "column_name");

                                addKeyRowsDropDownList.SelectedValue = hashObject[obj_3_RowsKey].ToString().Trim();
                                addNameRowsDropDownList.SelectedValue = hashObject[obj_4_RowsName].ToString().Trim();
        
                                #endregion Table
                                break;

                           case (int)Constants.Constants.ToolBox.SQL:
                                pSqlID.Visible = true;
                                addSql.Text = hashObject[obj_2_ValueKey].ToString().Trim();                              
                                break;

                           default:
                               break;
                       }

                       ShowHideAddObjectPanels(visibleTable);
                    }
                    catch(Exception ex)
                    {
                        //todo
                    }
                    #endregion
                }
                #endregion Select
                break;
            
            default:
                break;
        }
    }
    
    protected void addObjectSaveButton_Click(object sender, EventArgs e)   //newPanel
    {
        string result = string.Empty;

        Hashtable objectHashtable = new Hashtable();
        objectHashtable.Add(obj_1_TypeKey, string.Empty);
        objectHashtable.Add(obj_2_ValueKey, string.Empty);
        objectHashtable.Add(obj_3_RowsKey, string.Empty);
        objectHashtable.Add(obj_4_RowsName, string.Empty);

        int selectedValueID = 0;
        string selectedValue = addObjectDropDownList.SelectedValue.ToString();        
        int.TryParse(selectedValue, out selectedValueID);

        switch (selectedValueID)
        {
            case (int)Constants.Constants.ToolBox.Classifiers:
                objectHashtable[obj_1_TypeKey] = Constants.Constants.ToolBox.Classifiers.ToString();
                objectHashtable[obj_2_ValueKey] = addTableDropDownList.SelectedValue;
                break;

            case (int)Constants.Constants.ToolBox.TextBox:
                objectHashtable[obj_1_TypeKey] = Constants.Constants.ToolBox.TextBox.ToString();
                break;

            case (int)Constants.Constants.ToolBox.CheckBox:
                objectHashtable[obj_1_TypeKey] = Constants.Constants.ToolBox.CheckBox.ToString();
                break;

            case (int)Constants.Constants.ToolBox.DateTime:
                objectHashtable[obj_1_TypeKey] = Constants.Constants.ToolBox.DateTime.ToString();
                break;

            case (int)Constants.Constants.ToolBox.Table:
                objectHashtable[obj_1_TypeKey] = Constants.Constants.ToolBox.Table.ToString();
                objectHashtable[obj_2_ValueKey] = addTableDropDownList.SelectedValue;
                objectHashtable[obj_3_RowsKey] = addKeyRowsDropDownList.SelectedValue;
                objectHashtable[obj_4_RowsName] = addNameRowsDropDownList.SelectedValue;
                break;

            case (int)Constants.Constants.ToolBox.SQL:
                objectHashtable[obj_1_TypeKey] = Constants.Constants.ToolBox.SQL.ToString();
                objectHashtable[obj_2_ValueKey] = addSql.Text.Trim();
                break;

            default:
                break;
        }

        if (!string.IsNullOrEmpty(objectHashtable[obj_1_TypeKey].ToString()))
        {
            result = objectHashtable[obj_1_TypeKey].ToString()+objDelimiter
                + objectHashtable[obj_2_ValueKey]+objDelimiter
                + objectHashtable[obj_3_RowsKey]+objDelimiter
                + objectHashtable[obj_4_RowsName];

            int index = 0;
            string indexValue = addObjectHiddenFieldID.Value;
            int.TryParse(indexValue, out index);

            GridViewRow selectedRow = columnsGridView.Rows[index];
            int countColumn = columnsGridView.Columns.Count;
            TextBox objectTextBoxID = (TextBox)selectedRow.Cells[countColumn - 1].FindControl("addObjectTextBoxID");
            if (objectTextBoxID != null)
            {
                objectTextBoxID.Text = result.Replace("'",string.Empty);
                
                ClearControlAddObjectPanel();
            }
        }
    }
    
    protected void addObjectCancelButton_Click(object sender, EventArgs e) //newPanel
    {
        ClearControlAddObjectPanel();
    }
    
    private void ClearControlAddObjectPanel()
    {
        addObjectHiddenFieldID.Value = string.Empty;
        addColumnNameLabelID.Text = string.Empty;
        addObjectDropDownList.SelectedIndex = -1;
        addSql.Text = string.Empty;

        if (addTableDropDownList != null)
        {
            addTableDropDownList.SelectedIndex = -1;
        }

        if (addKeyRowsDropDownList != null)
        {
            addKeyRowsDropDownList.SelectedIndex = -1;
        }

        if (addNameRowsDropDownList != null)
        {
            addNameRowsDropDownList.SelectedIndex = -1;
        }

        addObjectDivID.Visible = false;
    }
    
    #endregion newPanel

    #region editPanel

    private void FillEditTableList()    //editPanel
    {
        try
        {
            DataTable sourceTable = GetListTableFromBD();

            Utils.FillSelector(editTableDDL, sourceTable, "name", "name");
            editTableDDL.SelectedValue = "**";
        }
        catch (Exception ex)
        {
            //todo
        }
    }

    private void FillEditObjectList()    //editPanel
    {
        try
        {
            DataTable editSourceTable = new DataTable();
            editSourceTable.Columns.Add("ObjectName", typeof(string));
            editSourceTable.Columns.Add("ObjectID", typeof(int));

            editSourceTable.NewRow();

            editSourceTable.Rows.Add(Constants.Constants.ToolBox.TextBox.ToString(), Constants.Constants.ToolBox.TextBox);
            editSourceTable.Rows.Add(Constants.Constants.ToolBox.Classifiers.ToString(), Constants.Constants.ToolBox.Classifiers);

            editSourceTable.Rows.Add(Constants.Constants.ToolBox.CheckBox.ToString(), Constants.Constants.ToolBox.CheckBox);
            editSourceTable.Rows.Add(Constants.Constants.ToolBox.DateTime.ToString(), Constants.Constants.ToolBox.DateTime);
            editSourceTable.Rows.Add(Constants.Constants.ToolBox.Table.ToString(), Constants.Constants.ToolBox.Table);

            editSourceTable.Rows.Add(Constants.Constants.ToolBox.SQL.ToString(), Constants.Constants.ToolBox.SQL);

            Utils.FillSelector(editObjectList, editSourceTable, "ObjectName", "ObjectID");
        }
        catch (Exception ex)
        {
            //todo
        }
    }

    protected void editTableDDL_SelectedIndexChanged(object sender, EventArgs e)  //editPanel
    {
        FillEditColumnsGridView();
        ClearEditCSVTextBox();
    }

    private void FillEditColumnsGridView()  //editPanel
    {
        string selectedValue = editTableDDL.SelectedValue.ToString();
        FillColumnsGridView(selectedValue, editColumnsGridView);
    }

    private void ClearEditCSVTextBox()
    {
        csvEditInsertTextBox.Text = string.Empty;
        csvEditUpdateTextBox.Text = string.Empty;
        csvEditSelectTextBox.Text = string.Empty;
    }
    
    private void ClearEditPanel()
    {
        //
    }

    protected void editSaveButton_Click(object sender, EventArgs e)
    {
        try
        {
            if (CheckEditValid())
            {
                string panelName = editPanelNameTextBox.Text.Trim();

                string csvInsert = csvEditInsertTextBox.Text;

                string csvUpdate = csvEditUpdateTextBox.Text;

                string csvSelect = csvEditSelectTextBox.Text;

                string csvObjects = GetObjectsList(editColumnsGridView, "editObjectTextBoxID");

                string csvAlias = GetAliasList(editColumnsGridView, "editAliasTextBoxID");

                string tableName = editTableDDL.SelectedValue.ToString();

                string csvSet = "csvInsert = '" + csvInsert + "' ";
                csvSet += ", csvUpdate ='" + csvUpdate + "' ";
                csvSet += ", csvSelect ='" + csvSelect + "' ";
                csvSet += ", csvObject = '" + csvObjects + "' ";
                csvSet += ", csvAlias = '" + csvAlias + "' ";
                csvSet += ", TableName = '" + tableName + "' ";

                string whereCondition = "PanelName = '" + panelName + "' ";

                if (mServerObject.UniversalUpdateFromValue(mTableName, csvSet, whereCondition))
                {
                    ShowPanel(listPanel.ID);
                }
                else
                {
                    Utils.InfoText(this, "Atentie", " { " + panelName + " } - nu a fost salvat. Verificati conectiunea cu internet ");
                }
            }
            else
            {
                Utils.InfoText(this, "Atentie", " Inserati toate cimpurile obligatorii !");
            }
        }
        catch (Exception ex)
        {
            Utils.InfoText(this, "Atentie", " { " + ex.Message + " } ");
        }
       
    }

    private bool CheckEditValid()  //newPanel
    {
        bool result = true;
        int countFalse = 0;

        if (string.IsNullOrEmpty(editPanelNameTextBox.Text))
        {
            countFalse++;
        }

        if (string.IsNullOrEmpty(csvEditInsertTextBox.Text))
        {
            countFalse++;
        }

        if (string.IsNullOrEmpty(csvEditUpdateTextBox.Text))
        {
            countFalse++;
        }

        if (string.IsNullOrEmpty(csvEditSelectTextBox.Text))
        {
            countFalse++;
        }

        if (countFalse != 0)
        {
            result = false;
        }
        return result;
    }

    protected void editCancelButton_Click(object sender, EventArgs e)
    {
        ShowPanel(listPanel.ID);
    }

    protected void editObjectGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        // If multiple ButtonField column fields are used, use the
        // CommandName property to determine which button was clicked.
        switch (e.CommandName)
        {
            case "Select":
                #region Select
                // Convert the row index stored in the CommandArgument
                // property to an Integer.
                editObjectDivID.Visible = true;
                int editIndex = Convert.ToInt32(e.CommandArgument);

                // Get the last name of the selected author from the appropriate
                // cell in the GridView control.
                GridViewRow editSelectedRow = editColumnsGridView.Rows[editIndex];
                int editCountColumn = editColumnsGridView.Columns.Count;
                TextBox editObjectTextBoxID = (TextBox)editSelectedRow.Cells[editCountColumn - 1].FindControl("editObjectTextBoxID");
                if (editObjectTextBoxID != null)
                {
                    editColumnNameLabelID.Text = editSelectedRow.Cells[0].Text;
                    editObjectHiddenFieldID.Value = editIndex.ToString();

                    #region
                    List<string> editListObject = new List<string>(editObjectTextBoxID.Text.Replace("'", "").Trim().Split(charObjDelimiter));
                    Hashtable hashObject = new Hashtable();
                    hashObject.Add(obj_1_TypeKey, editListObject[0]);
                    
                    if (editListObject.Count > 1)
                    {
                        hashObject.Add(obj_2_ValueKey, editListObject[1]);
                    }
                    else
                    {
                        hashObject.Add(obj_2_ValueKey, string.Empty);
                    }

                    if (editListObject.Count > 2)
                    {
                        hashObject.Add(obj_3_RowsKey, editListObject[2]);
                    }
                    else
                    {
                        hashObject.Add(obj_3_RowsKey, string.Empty);
                    }

                    if (editListObject.Count > 3)
                    {
                        hashObject.Add(obj_4_RowsName, editListObject[3]);
                    }
                    else
                    {
                        hashObject.Add(obj_4_RowsName, string.Empty);
                    }

                    try
                    {
                        int editTypeID = 0;
                        string editTypeObject = hashObject[obj_1_TypeKey].ToString();

                        #region
                        if (editTypeObject.Equals(Constants.Constants.ToolBox.TextBox.ToString()))
                        {
                            editTypeID = (int)Constants.Constants.ToolBox.TextBox;
                        }
                        if (editTypeObject.Equals(Constants.Constants.ToolBox.CheckBox.ToString()))
                        {
                            editTypeID = (int)Constants.Constants.ToolBox.CheckBox;
                        }
                        if (editTypeObject.Equals(Constants.Constants.ToolBox.DateTime.ToString()))
                        {
                            editTypeID = (int)Constants.Constants.ToolBox.DateTime;
                        }
                        if (editTypeObject.Equals(Constants.Constants.ToolBox.Classifiers.ToString()))
                        {
                            editTypeID = (int)Constants.Constants.ToolBox.Classifiers;
                        }
                        if (editTypeObject.Equals(Constants.Constants.ToolBox.Table.ToString()))
                        {
                            editTypeID = (int)Constants.Constants.ToolBox.Table;
                        }
                        if(editTypeObject.Equals(Constants.Constants.ToolBox.SQL.ToString()))
                        {
                            editTypeID = (int)Constants.Constants.ToolBox.SQL;
                        }
                        #endregion

                        bool editVisibleTable = false;
                        pEditKeyID.Visible = false;
                        pEditColumnNameID.Visible = false;
                        pEditSql.Visible = false;

                        DataTable editSourceTable = new DataTable();

                        editObjectList.SelectedValue = editTypeID.ToString();

                        switch (editTypeID)
                        {
                            case (int)Constants.Constants.ToolBox.Classifiers:
                                editVisibleTable = true;
                                editSourceTable = null;
                                editTableList.DataTextField = "Name";
                                editTableList.DataValueField = "Type ID";
                                editSourceTable = Utils.ModuleMain().GetClassifierTypesList();
                                Utils.FillSelector(editTableList, editSourceTable, "Name", "Type ID");

                                editTableList.SelectedValue = hashObject[obj_2_ValueKey].ToString();
                                break;

                            case (int)Constants.Constants.ToolBox.TextBox:
                                break;

                            case (int)Constants.Constants.ToolBox.CheckBox:
                                break;

                            case (int)Constants.Constants.ToolBox.DateTime:
                                break;

                            case (int)Constants.Constants.ToolBox.Table:
                                #region Table
                                editVisibleTable = true;

                                editTableList.DataTextField = "Name";
                                editTableList.DataValueField = "Name";
                                editSourceTable = null;
                                editSourceTable = Load_TableDropDown();
                                Utils.FillSelector(editTableList, editSourceTable, "Name", "Name");

                                string editTableSelectedValue = hashObject[obj_2_ValueKey].ToString().Trim();
                                editTableList.SelectedValue = editTableSelectedValue;

                                pEditKeyID.Visible = true;
                                pEditColumnNameID.Visible = true;

                                DataTable editSourceTableRows = mServerObject.GetColumnsByTableName(editTableSelectedValue);

                                Utils.FillSelector(editKeyRowsList, editSourceTableRows, "column_name", "column_name");
                                Utils.FillSelector(editNameRowsList, editSourceTableRows, "column_name", "column_name");

                                editKeyRowsList.SelectedValue = hashObject[obj_3_RowsKey].ToString().Trim();
                                editNameRowsList.SelectedValue = hashObject[obj_4_RowsName].ToString().Trim();

                                #endregion Table
                                break;

                            case (int)Constants.Constants.ToolBox.SQL:
                                #region SQL
                                pEditSql.Visible = true;
                                editSql.Text = hashObject[obj_2_ValueKey].ToString().Trim();
                                #endregion SQL
                                break;

                            default:
                                break;
                        }

                        ShowHideEditObjectPanels(editVisibleTable);
                    }
                    catch(Exception ex)
                    {
                        //todo
                    }
                    #endregion
                }
                #endregion Select
                break;

            default:
                break;
        }
    }

    protected void editObjectList_SelectedIndexChanged(object sender, EventArgs e)
    {
        string editSelectedValue = editObjectList.SelectedValue.ToString();
        int editSelectedValueID = 0;

        int.TryParse(editSelectedValue, out editSelectedValueID);

        bool editVisibleTable = false;
        pEditKeyID.Visible = false;
        pEditColumnNameID.Visible = false;
        pEditSql.Visible = false;

        DataTable editSourceTable = new DataTable();

        switch (editSelectedValueID)
        {
            case (int)Constants.Constants.ToolBox.Classifiers:
                editVisibleTable = true;
                editSourceTable = null;
                editTableList.DataTextField = "Name";
                editTableList.DataValueField = "Type ID";
                editSourceTable = Utils.ModuleMain().GetClassifierTypesList();
                Utils.FillSelector(editTableList, editSourceTable, "Name", "Type ID");

                break;

            case (int)Constants.Constants.ToolBox.TextBox:
                break;

            case (int)Constants.Constants.ToolBox.CheckBox:
                break;

            case (int)Constants.Constants.ToolBox.DateTime:
                break;

            case (int)Constants.Constants.ToolBox.Table:
                #region Table
                editVisibleTable = true;

                editTableList.DataTextField = "Name";
                editTableList.DataValueField = "Name";
                editSourceTable = null;
                editSourceTable = Load_TableDropDown();
                Utils.FillSelector(editTableList, editSourceTable, "Name", "Name");
                #endregion Table
                break;

            case (int)Constants.Constants.ToolBox.SQL:
                #region SQL
                pEditSql.Visible = true;
                #endregion SQL
                break;

            default:
                break;
        }

        ShowHideEditObjectPanels(editVisibleTable);
    }

    private void ShowHideEditObjectPanels(bool editVisibleTable)
    {
        pEditTableID.Visible = editVisibleTable;
    }

    protected void editDropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        string editObjectValue = editObjectList.SelectedValue.ToString();
        int editObjectID = 0;
        int.TryParse(editObjectValue, out editObjectID);

        pEditKeyID.Visible = false;
        pEditColumnNameID.Visible = false;
        pEditSql.Visible = false;

        if (editObjectID != (int)Constants.Constants.ToolBox.Classifiers)
        {
            string editSelectedValue = editTableList.SelectedValue.ToString();
            int editSelectedValueID = 0;
            int.TryParse(editSelectedValue, out editSelectedValueID);

            pEditKeyID.Visible = true;
            pEditColumnNameID.Visible = true;

            DataTable editSourceTable = mServerObject.GetColumnsByTableName(editSelectedValue);

            Utils.FillSelector(editKeyRowsList, editSourceTable, "column_name", "column_name");
            Utils.FillSelector(editNameRowsList, editSourceTable, "column_name", "column_name");
        }
    }

    protected void editObjectSaveButton_Click(object sender, EventArgs e)   //newPanel
    {
        string editSelectedValue = editObjectList.SelectedValue.ToString();
        int editSelectedValueID = 0;
        int.TryParse(editSelectedValue, out editSelectedValueID);

        string editResult = string.Empty;

        Hashtable objectHashtable = new Hashtable();
        objectHashtable.Add(obj_1_TypeKey, string.Empty);
        objectHashtable.Add(obj_2_ValueKey, string.Empty);
        objectHashtable.Add(obj_3_RowsKey, string.Empty);
        objectHashtable.Add(obj_4_RowsName, string.Empty);
        
        switch (editSelectedValueID)
        {
            case (int)Constants.Constants.ToolBox.Classifiers:
                objectHashtable[obj_1_TypeKey] = Constants.Constants.ToolBox.Classifiers.ToString();
                objectHashtable[obj_2_ValueKey] = editTableList.SelectedValue;
                break;

            case (int)Constants.Constants.ToolBox.TextBox:
                objectHashtable[obj_1_TypeKey] = Constants.Constants.ToolBox.TextBox.ToString();
                break;

            case (int)Constants.Constants.ToolBox.CheckBox:
                objectHashtable[obj_1_TypeKey] = Constants.Constants.ToolBox.CheckBox.ToString();
                break;

            case (int)Constants.Constants.ToolBox.DateTime:
                objectHashtable[obj_1_TypeKey] = Constants.Constants.ToolBox.DateTime.ToString();
                break;

            case (int)Constants.Constants.ToolBox.Table:
                objectHashtable[obj_1_TypeKey] = Constants.Constants.ToolBox.Table.ToString();
                objectHashtable[obj_2_ValueKey] = editTableList.SelectedValue;
                objectHashtable[obj_3_RowsKey] = editKeyRowsList.SelectedValue;
                objectHashtable[obj_4_RowsName] = editNameRowsList.SelectedValue;
                break;

            case (int)Constants.Constants.ToolBox.SQL:
                objectHashtable[obj_1_TypeKey] = Constants.Constants.ToolBox.SQL.ToString();
                objectHashtable[obj_2_ValueKey] = editSql.Text.Trim();
                break;

            default:
                break;
        }

        if (!string.IsNullOrEmpty(objectHashtable[obj_1_TypeKey].ToString()))
        {
            string objDelimiter = Constants.Constants.objectDelimiter;

            editResult = objectHashtable[obj_1_TypeKey].ToString() + objDelimiter
                + objectHashtable[obj_2_ValueKey] + objDelimiter
                + objectHashtable[obj_3_RowsKey] + objDelimiter
                + objectHashtable[obj_4_RowsName];

            int editIndex = 0;
            string editIndexValue = editObjectHiddenFieldID.Value;
            int.TryParse(editIndexValue, out editIndex);

            GridViewRow editSelectedRow = editColumnsGridView.Rows[editIndex];
            int editCountColumn = editColumnsGridView.Columns.Count;
            TextBox editObjectTextBoxID = (TextBox)editSelectedRow.Cells[editCountColumn - 1].FindControl("editObjectTextBoxID");
            if (editObjectTextBoxID != null)
            {
                editObjectTextBoxID.Text = editResult.Replace("'", string.Empty);

                ClearControlEditObjectPanel();
            }
        }
    }

    protected void editObjectCancelButton_Click(object sender, EventArgs e) //newPanel
    {
        ClearControlEditObjectPanel();
    }

    private void ClearControlEditObjectPanel()
    {
        editObjectHiddenFieldID.Value = string.Empty;
        editColumnNameLabelID.Text = string.Empty;
        editObjectList.SelectedIndex = -1;

        if (editTableList != null)
        {
            editTableList.SelectedIndex = -1;
        }

        if (editKeyRowsList != null)
        {
            editKeyRowsList.SelectedIndex = -1;
        }

        if (editNameRowsList != null)
        {
            editNameRowsList.SelectedIndex = -1;
        }

        if(editSql!=null)
        {
            editSql.Text = string.Empty;
        }

        editObjectDivID.Visible = false;
    }
    
    #endregion editPanel

    #region emptyPanel

    #endregion emptyPanel

    #region general function

    private DataTable GetListTableFromBD()
    {
        string csvColumns = "distinct name";
        string filterSelect = " xtype='U'";
        DataTable sourceTable = mServerObject.UniversalGetFromSingleTable("sysobjects", csvColumns, filterSelect);

        DataRow row;
        row = sourceTable.NewRow();
        row["name"] = "**";
        sourceTable.Rows.Add(row);

        // Accept changes.
        sourceTable.AcceptChanges();
        return sourceTable;
    }

    private void FillColumnsGridView(string selectedValue, GridView sourceGridView)
    {
        DataTable sourceTable = mServerObject.GetColumnsByTableName(selectedValue);
       
        sourceGridView.DataSource = sourceTable;
        sourceGridView.DataBind();       
    }

    protected DataTable Load_DropDown()
    {
        DataTable sourceTable = new DataTable();
        try
        {
            sourceTable = Utils.ModuleMain().GetClassifierTypesList();
        }
        catch (Exception ex)
        {
            //todo
        }
        return sourceTable;
    }

    protected DataTable Load_TableDropDown()
    {
        DataTable sourceTable = new DataTable();
        
        try
        {
            sourceTable = GetListTableFromBD();
            sourceTable.DefaultView.Sort = ("Name");
            sourceTable = sourceTable.DefaultView.ToTable();
        }
        catch (Exception ex)
        {
            //todo
        }

        return sourceTable;
    }

    private string GetObjectsList(GridView sourceGridView, string controlID) //newPanel
    {
        //controlID = "ClassifierList"
        string result = string.Empty;
        try
        {
            List<string> listResult = new List<string>();
            foreach (GridViewRow row in sourceGridView.Rows)
            {
                int countColumn = sourceGridView.Columns.Count;
                TextBox objectTextBoxID = (TextBox)row.Cells[countColumn - 1].FindControl(controlID);
                if (objectTextBoxID != null)
                {
                    listResult.Add(row.Cells[0].Text + "=" + objectTextBoxID.Text + "|");
                }
            }

            result = Crypt.Utils.ConvertListToString(listResult).Replace("'", string.Empty);
            
            if (result.Length > 0 && result.EndsWith("|"))
            {
                result = result.Substring(0, result.Length - 1);
            }
        }
        catch (Exception ex)
        {
            Utils.InfoText(this, "Erroare", ex.Message);
        }
        return result.Replace("|,","|");
    }

    private string GetAliasList(GridView sourceGridView, string controlID)   //newPanel
    {
        //controlID = aliasTextBoxID;
        string result = string.Empty;
        try
        {
            List<string> listResult = new List<string>();
            foreach (GridViewRow row in sourceGridView.Rows)
            {
                TextBox aliasString = row.FindControl(controlID) as TextBox;
                if (aliasString.Text.Length > 0)
                {
                    listResult.Add(aliasString.Text);
                }
                else
                {
                    listResult.Add(row.Cells[0].Text);
                }
            }
            result = Crypt.Utils.ConvertListToString(listResult).Replace("'", string.Empty);

        }
        catch (Exception ex)
        {
            Utils.InfoText(this, "Erroare", ex.Message);
        }

        return result;
    }

    #endregion general function
}